import educationList from 'service/config/picker/education.js'
import cityList from 'service/config/picker/city.js'
import relationList from 'service/config/picker/relation.js'
import verifys from 'service/util/verifys.js'

import { mapGetters, mapState } from 'vuex'

var cache = {
}

var vm = {
    data() {
        Object.assign(cache, {
            professionY: '360px',
            urgencyY: '600px',
            // 是否显示遮罩层
            showShadow: false,
            // 学历数据列表
            educationList,
            // 选中学历的vlaue值
            education: -1,
            // 选中学历的text值
            educationText: '',
            // 常用住址
            nowAddress: '',
            // 学历列表显示框 style.bottom 值，改变该值弹出选中框,
            // 亲属关系列表
            relationList: relationList.kinsfolk,
            // 社会关系列表
            societyList: relationList.society,
            // 亲属关系
            relationship: null,
            // 亲属手机号
            relationshipTel: '',
            // 亲属姓名
            relationshipName: '',
            // 社会关系
            socialRelation: null,
            // 社会关系手机号
            socialRelationTel: '',
            // 社会关系名字
            socialRelationName: '',
            educationY: '10000px',
            industryList: null,
            // 行业
            profession: null,
            // 行业名称
            professionName: null,
            // 月收入
            yearIncome: '',
            // 公司名称
            companyName: '',
            cityList,
            // 省份
            companyProvince: null,
            // 市级
            companyCity: null,
            // 公司地址
            companyAddress: '',
            professionOk: false,
            urgencyOk: false
        })
        return cache
    },
    computed: {
        ...mapState(['user']),
        ...mapGetters(['userPersonalStatus'])
    },
    mounted() {
        if (this.userPersonalStatus) {
            $loading.show()
            // 获取用户基本信息和职业列表信息
            $axios.all([$ajax.user.queryUserBase({ userId: this.user.userId }), $ajax.user.professionList({ userId: this.user.userId })])
                .then($axios.spread((userBaseRes, professionListRes) => {
                    var userBaseCode = userBaseRes.data.code
                    var userBaseData = userBaseRes.data.data
                    var professionListCode = professionListRes.data.code
                    var professionListData = professionListRes.data.data
                    if (userBaseCode == 'S' && professionListCode == 'S') {
                        Object.assign(cache, userBaseData)
                        for (var i = 0; i < professionListData.list.length; i++) {
                            var professionLi = professionListData.list[i]
                            if (professionLi.value == this.profession) {
                                this.professionName = professionLi.text
                                break
                            }
                        }
                    }
                    $loading.hide()
                })).catch(err => {
                    $loading.hide()
                    $router.replace('/system/neterror')
                })
        } else {
            $loading.show()
            $ajax.user.professionList({ userId: this.user.userId }).then(res => {
                var { code, data, msg } = res.data
                if (code == 'S') {
                    $loading.hide()
                    var { list } = data
                    this.industryList = [list]
                } else {
                    $toast.show(msg)
                    $router.push('/system/neterror')
                }
            }).catch(res => {
                $loading.hide()
                $router.replace('/system/neterror')
            })
        }

        // 关闭信息框
        this.closeEducationDo()
    },
    beforeDestroy(to, from, next) {
        if (this.$refs.professionPicker) {
            this.$refs.professionPicker.hide()
        }
        if (this.$refs.cityPicker) {
            this.$refs.cityPicker.hide()
        }
    },
    methods: {
        // 点击学历选择框中一项时，取学历值
        selectEducationDo(educationItem) {
            this.education = educationItem.value
            this.educationText = educationItem.text
        },
        // 打开学历选择框
        openEducationDo() {
            this.showShadow = true
            this.educationY = 0
        },
        // 关闭学历选择框
        closeEducationDo() {
            this.showShadow = false
            this.educationY = this.$refs.education.clientHeight + 'px'
        },
        // 打开职业信息框
        openProfessionDo() {
            this.showShadow = true
            this.professionY = 0
        },
        // 关闭职业信息框
        closeProfessionDo() {
            this.showShadow = false
            this.professionY = this.$refs.professionBox.clientHeight + 'px'
            this.professionOk = this.professionJudge()
        },
        // 打开紧急联系人信息框
        openUrgencyDo() {
            this.showShadow = true
            this.urgencyY = 0
        },
        // 关闭紧急联系人信息框
        closeUrgencyDo() {
            this.showShadow = false
            this.urgencyY = this.$refs.urgency.clientHeight + 'px'
            this.urgencyOk = this.urgencyJudge()
        },
        // 判断职业信息框是否输入完成
        professionJudge() {
            if (this.userPersonalStatus) {
                return false
            } else if (this.profession == null || this.companyProvince == null || this.companyCity == null) {
                return false
            } else {
                return verifys.verify(['yearIncome', 'companyName', 'companyAddress'], this, true)
            }
        },
        // 判断紧急联系人是否输入成功
        urgencyJudge() {
            return !this.userPersonalStatus && this.relationship != null && this.socialRelation != null && verifys.verify(['relationshipName', 'relationshipTel', 'socialRelationName', 'socialRelationTel'], this, true)
        },
        // 提交保存用户信息
        saveUserBaseDo() {
            if (this.education == -1) {
                $toast.show(verifys.msgs.education.required)
            } else if (!verifys.verify(['nowAddress'], this)) {
            } else if (this.profession == null) {
                $toast.show(verifys.msgs.profession.required)
            } else if (!verifys.verify(['yearIncome', 'companyName'], this)) {
            } else if (this.companyProvince == null || this.companyCity == null) {
                $toast.show(verifys.msgs.companyProvince.required)
            } else if (!verifys.verify(['companyAddress', 'relationshipName', 'relationshipTel'], this)) {
            } else if (this.relationship == null) {
                $toast.show(verifys.msgs.relationship.required)
            } else if (!verifys.verify(['socialRelationName', 'socialRelationTel'], this)) {
            } else if (this.socialRelation == null) {
                $toast.show(verifys.msgs.socialRelation.required)
            } else {
                var userId = this.user.userId
                var { education, nowAddress, profession, yearIncome, companyName, companyProvince, companyCity, companyAddress,
                    relationship, relationshipName, relationshipTel, socialRelation, socialRelationName, socialRelationTel } = this
                $loading.show()
                $ajax.user.saveUserBase({
                    userId,
                    education,
                    nowAddress,
                    profession,
                    yearIncome,
                    companyName,
                    companyProvince,
                    companyCity,
                    companyAddress,
                    relationship,
                    relationshipName,
                    relationshipTel,
                    socialRelation,
                    socialRelationName,
                    socialRelationTel
                }).then(res => {
                    var { code, msg } = res.data
                    $toast.show(msg)
                    if (code == 'S') {
                        $wxsdk.postLbs({
                            scene: 4,
                            mobile: this.user.mobile,
                            userId: this.user.userId
                        })
                        $router.replace('/loan/cacenter')
                    }
                }).catch(err => {
                    $toast.show('网络异常，请稍后重试')
                })
            }
        },
        industryInput() {
            setTimeout(() => {
                this.openProfessionDo()
            }, 300)
        },
        professionInput(value, text) {
            // 绑定省份和城市
            var texts = text.split(' ')
            this.companyProvince = texts[0]
            this.companyCity = texts[1]
            setTimeout(() => {
                this.openProfessionDo()
            }, 300)
        }
    },
    mixins: [$mixins.needLogin]
}

export default vm
